package ucuserslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/usercenter"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInviteConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserInviteConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInviteConfigLogic {
	return &UserInviteConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 获取用户支付账户列表
func (l *UserInviteConfigLogic) GetUserInviteConfigList(in *pb.UserInviteConfigReq) (*pb.UserInviteConfigResp, error) {
	defer utils.RecoverPanic()

	var err error
	var list []*usercenter.UserInviteConfig
	list, err = l.svcCtx.UserInviteConfigModel.Find(l.ctx)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	var rsp pb.UserInviteConfigResp
	rsp.List = make([]*pb.UserInviteConfig, 0)
	if len(list) <= 0 {
		return &rsp, nil
	}
	for i := 0; i < len(list); i++ {
		rsp.List = append(rsp.List, &pb.UserInviteConfig{
			Id:                     list[i].Id,
			SuperiorLevel:          list[i].SuperiorLevel,
			ReturnNewRegisterMoney: list[i].ReturnNewRegisterMoney,
			RechargeLimit:          list[i].RechargeLimit,
			RechargeReturnMoney:    list[i].RechargeReturnMoney,
			Link:                   list[i].Link,
			Enable:                 list[i].Enable,
			CreateTime:             list[i].CreateTime.Format(dbkey.DateTimeCNFormat),
		})
	}
	return &rsp, nil
}
